package udf.string;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.pig.data.DataBag;
import org.apache.pig.data.Tuple;

import util.io.FileInput;

public class ExtractNgramCounts {

	/**
	 * @param args
	 */
	
	
	static private Hashtable<String,LinkedList<String>> ordered = new Hashtable<String, LinkedList<String>>();

	public static void init(String path) {


		FileInput in =new FileInput(path);

		String line  = in.readString();

		while(line!=null){

			String t[]= line.split("\t");


			String hyphen = (String) t[0];


			//Order hyphen tokens

			List<String> order = Arrays.asList(hyphen.split("_"));
			Collections.sort(order);
			StringBuffer  sorted_= new StringBuffer();

			for(int i =0; i<order.size();i++){
				sorted_.append(order.get(i) + "_");
				//sorted = sorted +  order.get(i) + "_";

			}


			String sorted = sorted_.toString();
			sorted_=null;
			if(sorted.endsWith("_")){

				sorted = sorted.substring(0, sorted.length()-1);
			}

			//order.clear();
			//order=null;
			LinkedList<String> originals = new LinkedList<String>();

			if(ordered.containsKey(sorted)){

				originals =ordered.get(sorted);

				if(!originals.contains(hyphen)){
					originals.add(hyphen);
				}

				ordered.put(sorted, originals);

			}else{
				originals.add(hyphen);
				ordered.put(sorted, originals);

			}



			line =in.readString();
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
